<template>
  <div class="container">
    <div class="header">
      <h1>学院教务管理</h1>
    </div>
    <div class="content">
      <template>
        <a-table :pagination="false" :columns="columns" bordered :data-source="administrations">
          <template slot="Action" slot-scope="text, record">
            <a-popconfirm
                v-if="administrations.length"
                title="确定要重置密码吗？"
                @confirm="() => showResetPassword(record.account)"
            >
              <a href="javascript:;">重置密码</a>
            </a-popconfirm>

          </template>
        </a-table>
        <a-modal
            :visible="resetPasswordShow"
            :confirm-loading="confirmLoading"
            @ok="resetPassword"
            @cancel="resetPasswordShow=false"
            :width="300"
            centered
            title="请输入密码确认身份"
        >
          <a-input-password v-model="adminPassword"/>
        </a-modal>
      </template>
    </div>
  </div>
</template>
<script>
import administrations from "@/api/schoolAdmin/administrations";
import {resetPassword} from "@/api/users/password";
import {columns} from "@/pages/schoolAdmin/js/adminColumns";
export default {
  data() {
    return {
      administrations: [],
      resetPasswordShow: false,
      resetAccount: '',
      adminPassword: '',
      confirmLoading: false,
      columns:[],
      headers: {
        Authorization: localStorage.getItem("Authorization")
      }
    };
  },
  methods: {
    showResetPassword(account) {
      this.resetPasswordShow = true;
      this.resetAccount = account;
      this.adminPassword = "";
    },
    getAdministrations() {
      let self = this;
      administrations().then(res => {
        if (res.result) {
          self.administrations = res.administrations;
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    resetPassword() {
      if (!this.adminPassword) {
        this.$message.error("请输入密码");
        return;
      }
      this.confirmLoading = true;
      resetPassword(this.resetAccount, this.adminPassword)
          .then(res => {
            // 关闭对话框
            this.resetPasswordShow = false;
            this.confirmLoading = false;
            this.adminPassword = "";
            this.confirmLoading = false;
            if (res.result) {
              this.$message.success("重置密码成功");
            } else {
              this.$message.error(res.msg);
            }
          });
    },
  },
  mounted() {
    this.getAdministrations();
    this.columns = columns.adminColumns;
  }
};
</script>
<style scoped src="./css/common.css"></style>
<style scoped src="./css/adminManagement.css"></style>
